package com.lvcoding.clientfinal.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

//@Configuration
public class OAuth2LoginConfig {
    @Bean
    public ClientRegistrationRepository clientRegistrationRepository() {
        return new InMemoryClientRegistrationRepository(giteeClientRegistration(), aaaClientRegistration());
    }

    @Value("${spring.security.oauth2.client.registration.gitee.client-id}")
    private String clientId;
    @Value("${spring.security.oauth2.client.registration.gitee.client-secret}")
    private String clientSecret;
    @Value("${spring.security.oauth2.client.provider.gitee.token-uri}")
    private String tokenUri;
    @Value("${spring.security.oauth2.client.provider.gitee.authorization-uri}")
    private String authorizationUri;
    @Value("${spring.security.oauth2.client.registration.gitee.scope}")
    private String scope;
    @Value("${spring.security.oauth2.client.provider.gitee.user-info-uri}")
    private String userInfoUri;

    private ClientRegistration aaaClientRegistration() {
        return ClientRegistration.withRegistrationId("myauth")  // (1)
                .clientId("test")  // (2)
                .clientSecret("test")  // (3)
                .clientAuthenticationMethod(ClientAuthenticationMethod.POST)  // (4)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")  // (5)
                .clientName("myauth")       // (6)
                .tokenUri("http://auth-server:6006/oauth/token")  // (7)
                .authorizationUri("http://auth-server:6006/oauth/authorize")  // (8)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)  // (9)
                .scope("read")  // (10)
                .userNameAttributeName("name")  // (11)
                .userInfoUri("http://resource-server:6000/test/user")  // (12)
                .jwkSetUri("")  // (13)
                .build();
    }


    private ClientRegistration giteeClientRegistration() {
        return ClientRegistration.withRegistrationId("gitee")  // (1)
                .clientId(clientId)  // (2)
                .clientSecret(clientSecret)  // (3)
                .clientAuthenticationMethod(ClientAuthenticationMethod.POST)  // (4)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")  // (5)
                .clientName("gitee")       // (6)
                .tokenUri(tokenUri)  // (7)
                .authorizationUri(authorizationUri)  // (8)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)  // (9)
                .scope(scope)  // (10)
                .userNameAttributeName("name")  // (11)
                .userInfoUri(userInfoUri)  // (12)
                .jwkSetUri("")  // (13)
                .build();
    }

}
